from sqlalchemy.orm import sessionmaker, scoped_session
from orm01 import engine, Student
session_factory = sessionmaker(bind=engine)  # 获取session工厂类
Session = scoped_session(session_factory)  # 保证session单例,返回scoped_session对象
session1 = Session()
session2 = Session()
print(session1 is session2)  # True
# scoped_session实现了代理模式,可使用此对象直接操作,能够转发到代理对象中
student__all = Session.query(Student).all()  # 查询所有
# 查询个别字段,取第一个
student = Session.query(Student.id, Student.name).first()
# 查询年龄大于25的学生名字, one返回一个,如果结果不足或者多于1个会报错
students = Session.query(Student.name).filter(Student.age > 25).one()
# student__all = session1.query(Student).all()
print(students)

